﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace PatientInformationExtract.MainInterface.Models
{
    public class PatientListTreeItem : INotifyPropertyChanged,ICheckableItem
    {
        private PatientListTreeItemCollection _items;
        public PatientListTreeItemCollection Items
        {
            get
            { return this._items; }
            set
            {
                if (this._items != value)
                {
                    this._items = value;
                    NotifyPropertyChanged("Items");
                }
            }
        }

        private PatientListTreeItem _parent;
        public PatientListTreeItem Parent
        {
            get
            { return this._parent; }
            set
            {
                if (this._parent != value)
                {
                    this._parent = value;
                    NotifyPropertyChanged("Parent");
                }
            }
        }

        public ICheckableItem IParent
        {
            get
            {
                return this._parent;
            }
        }
        public ObservableCollection<ICheckableItem> IItems
        {
            get
            {
                return this._items;
            }
        }

        private string _header;
        public string Header
        {
            get
            {
                return this._header;
            }
            set
            {
                if (this._header != value)
                {
                    this._header = value;
                    NotifyPropertyChanged("Header");
                }
            }
        }

        private string _tag;
        public string Tag
        {
            get { return _tag; }
            set
            {
                if (_tag != value)
                {
                    _tag = value;
                    NotifyPropertyChanged("Tag");
                }
            }
        }

        private bool? _isChecked;
        public bool? IsChecked
        {
            get
            { return this._isChecked; }
            set
            {
                if (this._isChecked != value)
                {
                    this._isChecked = value;
                    NotifyPropertyChanged("IsChecked");
                    if (this._isChecked == true) // 如果节点被选中
                    {
                        if (this._items.Count != 0)
                            foreach (PatientListTreeItem item in this._items)
                                item.IsChecked = true;
                        if (this.Parent != null)
                        {
                            Boolean bExistUncheckedChildren = false;
                            foreach (PatientListTreeItem item in this.Parent.Items)
                                if (item.IsChecked != true)
                                {
                                    bExistUncheckedChildren = true;
                                    break;
                                }
                            if (bExistUncheckedChildren)
                                this.Parent.IsChecked = null;
                            else
                                this.Parent.IsChecked = true;
                        }
                    }
                    else if (this._isChecked == false)   // 如果节点未选中
                    {
                        if (this.Items.Count != 0)
                            foreach (PatientListTreeItem item in this._items)
                                item.IsChecked = false;
                        if (this.Parent != null)
                        {
                            Boolean bExistCheckedChildren = false;
                            foreach (PatientListTreeItem item in this.Parent.Items)
                                if (item.IsChecked != false)
                                {
                                    bExistCheckedChildren = true;
                                    break;
                                }
                            if (bExistCheckedChildren)
                                this.Parent.IsChecked = null;
                            else
                                this.Parent.IsChecked = false;
                        }
                    }
                    else
                    {
                        if (this.Parent != null)
                            this.Parent.IsChecked = null;
                    }
                }
            }
        }

        public PatientListTreeItem()
        {
            this.Items = new PatientListTreeItemCollection(this);
            this._isChecked = false;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
